﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ElingOA6_Admin_DSJCJ.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace ElingOA6_Admin_DSJCJ.Areas.Admin.Controllers
{
    /// <summary>
    /// 房屋买卖财政补贴
    /// </summary>
    [OutputCache(Duration = 0, VaryByParam = "none")]
    public class FwmmszbtController : AdminBaseController
    {
        //
        // GET: /Admin/Fwmmszbt/
        /// <summary>
        /// 房屋买卖财政补贴主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_Fwmmszbt", Order = 1)]
        [LogPageActionFilter("房屋买卖财政补贴", Order = 2)]
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 数据列表
        /// </summary>       
        /// <param name="yyyy">年</param>
        /// <param name="mm">月</param>
        /// <param name="nsr">纳税人</param>
        /// <param name="sfz">身份证</param>
        /// <param name="btbl">比例</param>
        /// <param name="khyh">开户银行</param>
        /// <param name="btrq">日期</param>
        /// <param name="sz">税种</param>
        /// <returns>视图</returns>
        public ActionResult FwmmszbtList(string yyyy, string mm, string nsr, string sfz, string btbl, string khyh, string btrq, string sz)
        {
            int pageIndex = 1;
            ViewData["yyyy"] = yyyy;
            ViewData["mm"] = mm;
            ViewData["nsr"] = nsr;
            ViewData["sfz"] = sfz;
            ViewData["btbl"] = btbl;
            ViewData["khyh"] = khyh;
            ViewData["btrq"] = btrq;
            ViewData["sz"] = sz;
            List<Model.Fwmmszbt> fwmmszbtList = GetFwmmszbtList(pageIndex);

            return View(fwmmszbtList);
        }

        /// <summary>
        /// 获取数据
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageCount">每页显示大小</param>
        /// <param name="pageRowSize">每页显示页数</param>
        /// <returns>数据集合</returns>
        private List<Model.Fwmmszbt> GetFwmmszbtList(int pageIndex, int pageCount = 0, int pageRowSize = 0)
        {
            BLL.Fwmmszbt fwmmszbtBll = new BLL.Fwmmszbt();

            BLL.Common commonBll = new BLL.Common();
            string fiedls = "*";
            int total;
            string where = "sz<>'自住改善型'";

            string yyyy = Request.Params["yyyy"];
            string mm = Request.Params["mm"];
            string nsr = Request.Params["nsr"];
            string sfz = Request.Params["sfz"];
            string btbl = Request.Params["btbl"];
            string khyh = Request.Params["khyh"];
            string btrq = Request.Params["btrq"];
            string sz = Request.Params["sz"];

            if (yyyy != null && yyyy != "")
            {
                where += "and datepart(year,btrq) ='" + yyyy + "'";
            }
            if (mm != null && mm != "")
            {
                where += "and datepart(mm,btrq) ='" + mm + "'";
            }
            if (nsr != null && nsr != "")
            {
                where += "and nsr like '%" + nsr + "%'";
            }
            if (sfz != null && sfz != "")
            {
                where += "and sfz ='" + sfz + "'";
            }
            if (btbl != null && btbl != "")
            {
                where += "and btbl = '" + btbl + "'";
            }
            if (khyh != null && khyh != "")
            {
                where += "and khyh = '" + khyh + "'";
            }
            if (btrq != null && btrq != "")
            {
                where += " and btrq = convert(datetime,'" + btrq + "') ";
            }
            if (sz != null && sz != "")
            {
                where += " and sz = '" + sz + "'";
            }
            DataSet ds = commonBll.GetListByPage("[nc].nc_fwmmszbt", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, where, "btrq", 1, out total);

            List<Model.Fwmmszbt> fwmmszbtAllList = new List<Model.Fwmmszbt>();
            List<Model.Fwmmszbt> fwmmszbtList = fwmmszbtBll.DataTableToListFwmmszbtz(ds.Tables[0]);

            pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                fwmmszbtAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (fwmmszbtList.Count > i)
                {
                    fwmmszbtAllList[((pageIndex - 1) * pageRowSize) + i] = fwmmszbtList[i];
                }
            }
            return fwmmszbtAllList;
        }

        /// <summary>
        /// 用于页面的刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult FwmmszbtListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string id = Request.Params["id"];
            string callbackArg = DevExpressHelper.CallbackArgument;
            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();
            string yyyy = Request.Params["yyyy"];
            string mm = Request.Params["mm"];
            string nsr = Request.Params["nsr"];
            string sfz = Request.Params["sfz"];
            string btbl = Request.Params["btbl"];
            string khyh = Request.Params["khyh"];
            string btrq = Request.Params["btrq"];
            string sz = Request.Params["sz"];

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }

            List<Model.Fwmmszbt> fwmmszbtList = GetFwmmszbtList(pageIndex, pageCount, pageRowSize);


            return PartialView("FwmmszbtListPartial", fwmmszbtList);

        }


        /// <summary>
        /// 初始化页面
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>视图</returns>
        public ActionResult FwmmszbtContent(string id)
        {
            Model.Fwmmszbt fwmmszbtModel = new Model.Fwmmszbt();
            BLL.Fwmmszbt fwmmszbtBll = new BLL.Fwmmszbt();
            if (string.IsNullOrEmpty(id))
            {
                fwmmszbtModel.khyh = "鄞州银行";
                fwmmszbtModel.btrq = DateTime.Now;
                fwmmszbtModel.sz = "个人所得税";
                ViewData["sfz"] = "0";
                ViewData["type"] = "ADD";
            }
            else
            {
                id = Fetch.estimateString(id, CheckGetEnum.Safety);
                fwmmszbtModel = fwmmszbtBll.GetModel(decimal.Parse(id));
                ViewData["id"] = id;
                ViewData["sfz"] = "1";
                ViewData["type"] = "Edit";
            }


            return View(fwmmszbtModel);

        }

        /// <summary>
        /// 保存修改
        /// </summary>
        /// <param name="fwmmszbtModel">model对象</param>
        /// <returns>视图</returns>
        public ActionResult SaveFwmmszbt(Model.Fwmmszbt fwmmszbtModel)
        {


            if (Fetch.Post("hidid") == "" || Fetch.Post("hidid") == null)
            {
                fwmmszbtModel.id = fwmmszbtModel.id;
            }
            else
            {
                fwmmszbtModel.id = decimal.Parse(Fetch.Post("hidid"));
            }
            fwmmszbtModel.btbl = decimal.Parse(Fetch.Post("tbbtbl"));
            fwmmszbtModel.btrq = DateTime.Parse(Fetch.Post("tbbtrq"));
            fwmmszbtModel.khyh = Fetch.Post("tbkhyh");
            fwmmszbtModel.nsr = Fetch.Post("tbnsr");
            fwmmszbtModel.sfz = Fetch.Post("tbsfz");
            fwmmszbtModel.sz = RadioButtonListExtension.GetValue<string>("tbsz");
            fwmmszbtModel.ybtse = decimal.Parse(Fetch.Post("tbybtse"));
            fwmmszbtModel.yjsk = decimal.Parse(Fetch.Post("tbyjsk"));
            fwmmszbtModel.zh = Fetch.Post("tbzh");


            string type = Fetch.Post("hdtype");
            bool exist = false;//不存在
            ActionResult actionResult = null;
            BLL.Fwmmszbt fwmmszbtBll = new BLL.Fwmmszbt();

            try
            {
                if (type == "ADD")
                {
                    if (fwmmszbtBll.Exists(fwmmszbtModel.sfz))
                    {
                        exist = true;
                    }
                    else
                    {
                        //增加
                        fwmmszbtBll.Add(fwmmszbtModel);
                        exist = false;
                    }

                }
                else if (type == "Edit")
                {
                    //修改
                    fwmmszbtBll.Update(fwmmszbtModel);
                    exist = false;
                }

                if (!exist)
                {
                    ViewData["message"] = "保存成功";
                    ViewData["IsClose"] = "Y";
                    actionResult = View("FwmmszbtContent", fwmmszbtModel);
                }
                else
                {
                    ViewData["message"] = "身份证已存在,保存失败";
                    ViewData["IsClose"] = "N";
                    actionResult = View("FwmmszbtContent", fwmmszbtModel);
                }


            }
            catch (Exception)
            {

                ViewData["message"] = "保存失败";
                ViewData["IsClose"] = "N";
                actionResult = View("FwmmszbtzContent", fwmmszbtModel);

            }

            return actionResult;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">编号</param>
        /// <returns>返回str</returns>
        public string Delete(string id)
        {
            string str = string.Empty;
            id = Fetch.estimateString(id, CheckGetEnum.Safety);
            BLL.Fwmmszbt fwmmszbtBll = new BLL.Fwmmszbt();
            try
            {
                fwmmszbtBll.Delete(decimal.Parse(id));
                str = "ok";

            }
            catch (Exception)
            {
                str = "error";

            }
            return str;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids">主键</param>
        /// <returns>是否删除</returns>
        public string DeleteMany(string ids)
        {
            string str = string.Empty;
            string[] idArray = ids.Split(new string[] { "%!^" }, StringSplitOptions.RemoveEmptyEntries);
            BLL.Fwmmszbt fwmmszbtBll = new BLL.Fwmmszbt();
            try
            {
                fwmmszbtBll.DeleteMany(idArray);
                str = "ok";

            }
            catch (Exception)
            {
                str = "error";

            }
            return str;
        }

    }
}
